<?php
namespace app\admin\controller;
use app\comModel\model\Works;
use think\Controller;
use think\Request;

class Index extends Controller
{
    public function index()
    {
        //echo 11111;
       // $list=Db::name('works')->select();
//        $list=Db::name('works')->where('isdelete',0)->paginate(2);
        $works=new Works;
        //Work::where('isdelete',0)->paginate(10);

        $list=$works->where('isdelete',0)->paginate(10);
        //var_dump($list);
        $this->assign('list', $list);
        return $this->fetch('index');
        //return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
    }
    public function update(){
        $id=Request::instance()->param('id');
        //var_dump($id);exit;
        if(is_numeric($id) && $id>0){
            //$data=Db::name('works')->where('id',$id)->find();
            $data=Works::get($id);
            //var_dump($data);
            $this->assign('data',$data);
            return $this->fetch('index/update');
        }
    }
    public function updateWork(){

        if(Request::instance()->isPost()){
            $id=input("post.id");
            //var_dump($id);exit;
            $title=input("post.title");
            $type=input("post.type");
            $content=input("post.content");
            $status=input("post.status");
            $dir = ROOT_PATH . 'public/uploads';
            $img_url='';
            // return $dir;exit;
            $createDir=is_dir($dir) OR mkdir($dir, 0777, true);
            if(is_dir($dir)){
                // 获取表单上传文件
                $file = request()->file('img_url');
                // 移动到框架应用根目录/public/uploads/ 目录下
                if($file){
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                    if($info){
                        // 成功上传后 获取上传信息
                        // 输出 jpg
                        //echo $info->getExtension();
                        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                        // echo $info->getSaveName();
                        // 输出 42a79759f284b767dfcb2a0197904287.jpg
                        $img_url=$info->getSaveName();
                    }else{
                        // 上传失败获取错误信息
                        echo $file->getError();
                    }
                }
//            foreach($files as $file){
//                // 移动到框架应用根目录/public/uploads/ 目录下
//                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
//                if($info){
//                    // 成功上传后 获取上传信息
//                    // 输出 jpg
//                    echo $info->getExtension();
//                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                    echo $info->getFilename();
//                }else{
//                    // 上传失败获取错误信息
//                    echo $file->getError();
//                }
//            }
            }

            $data=[
                'title'=>$title,
                'type'=>$type,
                'sm_img'=>$img_url,
                'content'=>$content,
                'pid'=>$id,
                'status'=>$status,
                'isdelete'=>0,
                'serviceManager_id'=>1,
                'update_times'=>time()
            ];
       // var_dump($data);exit;
//        $data=$_POST;
      //  var_dump($data);
//            $add_work=Db::name('works')->where('id',$id)->update($data);
            $works=new Works;
            $add_work=$works->save($data,['id'=>$id]);
            if($add_work){
                echo "成功";
            }else{
                echo "失败";
            }
        }
    }
}
